package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_wjjhl.domain.WdclwcxkyqdBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.WdclwcxkyqdBaDetailVo;
import com.ruoyi.report_wjjhl.mapper.WdclwcxkyqdBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdclycsjBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.WdclycsjBsInfoVo;
import com.ruoyi.report_wjjhl.domain.WdclycsjBsInfo;
import com.ruoyi.report_wjjhl.mapper.WdclycsjBsInfoMapper;
import com.ruoyi.report_wjjhl.service.IWdclycsjBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 无机结合料稳定材料延迟时间试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-08-19
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdclycsjBsInfoServiceImpl implements IWdclycsjBsInfoService {

    private final WdclycsjBsInfoMapper baseMapper;
    private final WdclwcxkyqdBaDetailMapper detailMapper;


    /**
     * 查询无机结合料稳定材料延迟时间试验检测记录
     */
    @Override
    @FIllBasicInfo
    public WdclycsjBsInfoVo queryById(Long excelId){
        WdclycsjBsInfoVo wdclycsjBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<WdclwcxkyqdBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<WdclwcxkyqdBaDetail>()
            .eq(WdclwcxkyqdBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            wdclycsjBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return wdclycsjBsInfoVo;
    }

    /**
     * 查询无机结合料稳定材料延迟时间试验检测记录列表
     */
    @Override
    public TableDataInfo<WdclycsjBsInfoVo> queryPageList(WdclycsjBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdclycsjBsInfo> lqw = buildQueryWrapper(bo);
        Page<WdclycsjBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询无机结合料稳定材料延迟时间试验检测记录列表
     */
    @Override
    public List<WdclycsjBsInfoVo> queryList(WdclycsjBsInfoBo bo) {
        LambdaQueryWrapper<WdclycsjBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdclycsjBsInfo> buildQueryWrapper(WdclycsjBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdclycsjBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, WdclycsjBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, WdclycsjBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), WdclycsjBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), WdclycsjBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), WdclycsjBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), WdclycsjBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), WdclycsjBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), WdclycsjBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), WdclycsjBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), WdclycsjBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), WdclycsjBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), WdclycsjBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), WdclycsjBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), WdclycsjBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), WdclycsjBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), WdclycsjBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), WdclycsjBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), WdclycsjBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), WdclycsjBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), WdclycsjBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), WdclycsjBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(bo.getShijsl() != null, WdclycsjBsInfo::getShijsl, bo.getShijsl());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), WdclycsjBsInfo::getShijcc, bo.getShijcc());
        lqw.eq(StringUtils.isNotBlank(bo.getShijzbff()), WdclycsjBsInfo::getShijzbff, bo.getShijzbff());
        lqw.eq(bo.getChengxysd() != null, WdclycsjBsInfo::getChengxysd, bo.getChengxysd());
        lqw.eq(bo.getJiazsl() != null, WdclycsjBsInfo::getJiazsl, bo.getJiazsl());
        lqw.eq(bo.getZuidgmd() != null, WdclycsjBsInfo::getZuidgmd, bo.getZuidgmd());
        lqw.eq(bo.getZuijhsl() != null, WdclycsjBsInfo::getZuijhsl, bo.getZuijhsl());
        lqw.eq(bo.getWendjjl() != null, WdclycsjBsInfo::getWendjjl, bo.getWendjjl());
        lqw.eq(StringUtils.isNotBlank(bo.getKuanglcpbl()), WdclycsjBsInfo::getKuanglcpbl, bo.getKuanglcpbl());
        lqw.eq(StringUtils.isNotBlank(bo.getZhijrq()), WdclycsjBsInfo::getZhijrq, bo.getZhijrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyrq()), WdclycsjBsInfo::getShiyrq, bo.getShiyrq());
        lqw.eq(bo.getLingq() != null, WdclycsjBsInfo::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getChengxsj()), WdclycsjBsInfo::getChengxsj, bo.getChengxsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJinssj()), WdclycsjBsInfo::getJinssj, bo.getJinssj());
        lqw.eq(bo.getYancsjNum() != null, WdclycsjBsInfo::getYancsjNum, bo.getYancsjNum());
        lqw.eq(bo.getPingjkyqd() != null, WdclycsjBsInfo::getPingjkyqd, bo.getPingjkyqd());
        lqw.eq(bo.getBaozl() != null, WdclycsjBsInfo::getBaozl, bo.getBaozl());
        lqw.eq(bo.getBaozlxsza() != null, WdclycsjBsInfo::getBaozlxsza, bo.getBaozlxsza());
        lqw.eq(bo.getPiancxscv() != null, WdclycsjBsInfo::getPiancxscv, bo.getPiancxscv());
        lqw.eq(bo.getZuidz() != null, WdclycsjBsInfo::getZuidz, bo.getZuidz());
        lqw.eq(bo.getZuixz() != null, WdclycsjBsInfo::getZuixz, bo.getZuixz());
        lqw.eq(bo.getBiaozcs() != null, WdclycsjBsInfo::getBiaozcs, bo.getBiaozcs());
        lqw.eq(bo.getDaibz() != null, WdclycsjBsInfo::getDaibz, bo.getDaibz());
        return lqw;
    }

    /**
     * 新增无机结合料稳定材料延迟时间试验检测记录
     */
    @Override
    public Boolean insertByBo(WdclycsjBsInfoBo bo) {
        WdclycsjBsInfo add = BeanUtil.toBean(bo, WdclycsjBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 13次
            ArrayList<WdclwcxkyqdBaDetail> details = new ArrayList<>(13);
            for (long i = 1; i <= 13L; i++) {
                details.add(new WdclwcxkyqdBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改无机结合料稳定材料延迟时间试验检测记录
     */
    @Override
    public Boolean updateByBo(WdclycsjBsInfoBo bo) {
        WdclycsjBsInfo update = BeanUtil.toBean(bo, WdclycsjBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), WdclwcxkyqdBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdclycsjBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除无机结合料稳定材料延迟时间试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
